﻿using System.IO;

namespace ShuttleBus.Transports.Msmq
{
    public class MsmqMessage : BaseMessage
    {
        private readonly System.Messaging.Message _message;
        private StreamWrapper _memoryStream;

        public MsmqMessage(System.Messaging.Message message)
        {
            _message = message;
        }

        public override string Id
        {
            get { return _message.Id; }
        }

        public override IStream Contents
        {
            get
            {
                var o = _message.Formatter.Read(_message);

                if (_memoryStream == null)
                {
                    var memoryStream = new MemoryStream();
                    var writer = new StreamWriter(memoryStream);
                    writer.WriteLine(o);
                    writer.Flush();
                    memoryStream.Flush();
                    memoryStream.Position = 0;
                    _memoryStream = new StreamWrapper(memoryStream);
                }

                return _memoryStream;
            }
        }

        public override string ResponseQueue
        {
            get { return string.Empty; }
        }

        public override int RecieveCount
        {
            get { return 0; }
        }

        public override object OriginalMessage
        {
            get { return _message; }
        }
    }
}